program add_star
	tostring `1', replace usedisplayformat force
	replace `1' = cond(`2' < 0.01, `1' + "$^{***}$", ///
		cond(`2' < 0.05, `1' + "$^{**}$", /// 
		cond(`2' < 0.1, `1' + "$^{*}$", `1'))) 
	drop `2'
end

	
*------------------------------------------------------------------------------*
* Summary stat (Level differences) 
*------------------------------------------------------------------------------*

// Choose one (or none for "all mortgages")
*global tabsample 
global tabsample ftb
 	

use "${datadir}\HTB_main", replace

if "${tabsample}" == "ftb" {
	keep if advtype == 3
}
if "${tabsample}" == "hm" {
	keep if advtype == 4
}
	
// Collapse
preserve
	collapse_vars htb_dummy
	save "${tabledir}\descriptive_mean${tabsample}", replace
restore

preserve
	collapse_vars_count htb_dummy
	rename v1 v1_count
	rename v2 v2_count
	save "${tabledir}\descriptive_count${tabsample}", replace
restore

// put together the table
collapse_vars_sd htb_dummy
table_diff htb_dummy

prepare_ttest_result
add_parenthesis *_sd

table_diff_format ${tabsample}
latexTable

if "${tabsample}" != "" {
	drop if regexm(_varname, "First time")
} 
// Paper
outsheet using "${tabledir}\descriptive_diff${tabsample}_1.tex" ///
	if regexm(_varname, "Age|First time|quad Gross income"), replace noquote nonames
	
outsheet using "${tabledir}\descriptive_diff${tabsample}_2.tex" ///
	if regexm(_varname,"value|Down|Equity|Interest|Maturity|fixed"), ///
	replace noquote nonames
	
outsheet using "${tabledir}\descriptive_diff${tabsample}_3.tex" ///
	if regexm(_varname, "LTV|LTI|Payment"), replace noquote nonames


// Last row of the table with n obs
table_diff_last_row
latexTable
outsheet using "${tabledir}\num_diff${tabsample}.tex", ///
	replace noquote nonames
	

*------------------------------------------------------------------------------*	
* Summary stat: Interest rate charged 
*------------------------------------------------------------------------------*	

use "${datadir}\HTB_main", replace

gen interest_htb = interest if htb_dummy
gen interest_nhtb = interest if !htb_dummy
tw (kdensity interest_htb, bw(0.25) lwidth(medthick)) ///
	(kdensity interest_nhtb, bw(0.25) lpattern(dash) lwidth(medthick)), ///
	graphregion(color(white)) xtitle("Interest (%)") ylabel(, angle(0)) ///
	leg(order(1 "EL" 2 "non-EL"))


*------------------------------------------------------------------------------*
* Summary stat (Level differences controlling for region and year) 
*------------------------------------------------------------------------------*

// Choose one
*global tabsample 
global tabsample ftb
*global tabsample hm


use "${datadir}\HTB_main", replace
if "${tabsample}" == "ftb" {
	keep if advtype == 3
}
if "${tabsample}" == "hm" {
	keep if advtype == 4
}

gen nohtb = !htb_dummy

capture erase "${datadir}\num_diff_reg.dta"
foreach x of varlist $tabvarlist {
	if "`x'" == "equity" {
		reg `x' htb_dummy b6.region b2016.year, nocons	
		matrix B = r(table)
		matrix A = (_b[htb_dummy], _se[htb_dummy], 0, 0, ///	
			_b[htb_dummy], _se[htb_dummy], B[4, 1]) // last one is pvalue
	}
	else if inlist("`x'", "combined_LTV", "combined_LTI") {
		reg `x' b6.region b2016.year if htb_dummy
		matrix A = (_b[_cons], _se[_cons])
		scalar n1 = e(N)		// for standard error later
		est store reg1
		
		local variable = subinstr("`x'", "combined_", "", .)
		reg `variable' htb_dummy b6.region b2016.year		
		matrix B = (_b[_cons], _se[_cons])
		scalar n2 = e(N)
		est store reg2
		
		
		suest reg1 reg2
		test [reg1_mean]_cons=[reg2_mean]_cons
		local pvalue = r(p)
		
		local diff_coeff = [reg1_mean]_cons-[reg2_mean]_cons
		
		scalar SE1 = A[1,2]
		scalar SE2 = B[1,2]
		local diff_se = sqrt( (n1 * SE1^2 + n2 * SE2^2 ) / (n1 + n2 -2) )
		
		matrix A = (A, B, `diff_coeff', `diff_se', `pvalue')
	}
	else {
		reg `x' htb_dummy b6.region b2016.year
		matrix B = r(table)
		lincom _b[_cons] + htb_dummy
		matrix A = (r(estimate), r(se), _b[_cons], _se[_cons], ///	
			_b[htb_dummy], _se[htb_dummy], B[4, 1]) // last one is pvalue
	}
	
	svmat A
	
	preserve
		keep A*
		keep in 1
		capture append using "${datadir}\num_diff_reg"
		save "${datadir}\num_diff_reg", replace
	restore
	
	drop A*
}	

use "${datadir}\num_diff_reg", clear

gen sortorder = _N - _n
sort sortorder
drop sortorder

gen _varname = ""
local n = _N
forvalues i = 1/`n' {
	local a: word `i' of $tabvarlist
	replace _varname = "`a'" in `i'
}
table_diff_format ${tabsample}
order _varname
format A* %4.2fc
add_parenthesis A2 A4 A6
add_star A5 A7

drop A6	
latexTable

if "${tabsample}" != "" {
	drop if regexm(_varname, "First time")
} 
// Paper
outsheet using "${tabledir}\descriptive_ctrl${tabsample}_1.tex" ///
	if regexm(_varname, "Age|First time|quad Gross income"), replace noquote nonames
	
outsheet using "${tabledir}\descriptive_ctrl${tabsample}_2.tex" ///
	if regexm(_varname,"value|Down|Equity|Interest|Maturity|fixed"), ///
	replace noquote nonames
	
outsheet using "${tabledir}\descriptive_ctrl${tabsample}_3.tex" ///
	if regexm(_varname, "LTV|LTI|Payment"), replace noquote nonames
	
	
	
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC	
* Summary stat (Diff-in-dff with London HTB) *
*------------------------------------------------------------------------------*

// Choose one (or none for "all mortgages")
*global tabsample outlondon
global tabsample southeast
*global tabsample london
 	

use "${datadir}\HTB_main_cleaned_feb2016", replace

if "${tabsample}" == "london" {
	keep if region == 5
}
if "${tabsample}" == "outlondon" {
	keep if region != 5
}
if "${tabsample}" == "southeast" {
	keep if region == 6
}

order ${tabvarlist}	// for tables formatting
	
// Collapse
preserve
	collapse_vars post
	save "${tabledir}\descriptive_mean${tabsample}", replace
restore

preserve
	collapse_vars_count post
	rename v1 v1_count
	rename v2 v2_count
	save "${tabledir}\descriptive_count${tabsample}", replace
restore

// put together the table
collapse_vars_sd post
table_diff post

prepare_ttest_result
add_parenthesis *_sd 

table_diff_format
qui: latexTable
	
outsheet using "${tabledir}\descriptive_diff_perc${tabsample}_1.tex" ///
	if regexm(_varname, "Age|First time|quad Gross income"), replace noquote nonames
	
outsheet using "${tabledir}\descriptive_diff_perc${tabsample}_2.tex" ///
	if regexm(_varname,"value|Down|Equity|Interest|Maturity|fixed"), ///
	replace noquote nonames
	
outsheet using "${tabledir}\descriptive_diff_perc${tabsample}_3.tex" ///
	if regexm(_varname, "LTV|LTI|Payment"), replace noquote nonames
	
table_diff_last_row post
latexTable
outsheet using "${tabledir}\num_diff_perc${tabsample}.tex", ///
	replace noquote nonames
	
	
*------------------------------------------------------------------------------*
* Summary stat (Diff-in-dff with Border) *
*------------------------------------------------------------------------------*

// Choose one (or none for "all mortgages")
*global tabsample borderin
global tabsample borderout
*global tabsample london
 	
	
use "${datadir}\HTB_main_cleaned_feb2016", replace


drop _merge
gen LAD14CD = laua
sort LAD14CD
merge m:1 LAD14CD using "${datadir}\gbdb.dta" 
keep if _merge==3 | LAD14NM =="City of London" | LAD14NM == "Westminster" | LAD14NM == "Camden" | LAD14NM == "Islington" | ///
					LAD14NM =="Elmbridge" | LAD14NM =="Oxford"

HERE HERE
gen treat = .						
replace treat = 0 if 	LAD14NM == "Epping Forest" 	| LAD14NM == "Broxbourne" | ///
						LAD14NM == "Welwyn Hatfield" 	| LAD14NM == "Hertsmere" | ///
						LAD14NM == "Three Rivers" 	| LAD14NM == "South Bucks" | ///
						LAD14NM == "Spelthorne" 	| LAD14NM == "Elmbridge" | ///
						LAD14NM == "Epsom and Ewell" 	| LAD14NM == "Reigate and Banstead" | ///
						LAD14NM == "Tandridge" 	| LAD14NM == "Sevenoaks" | ///
						LAD14NM == "Dartford" 	| LAD14NM == "Thurrock" | ///
						LAD14NM == "Brentwood"  | LAD14NM == "Slough" | ///
						LAD14NM == "Thurrock" 
replace treat = 1 if 	LAD14NM == "Barnet" | LAD14NM == "Bexley" | ///
						LAD14NM == "Bromley" | LAD14NM == "Croydon" | ///
						LAD14NM == "Enfield" | LAD14NM == "Harrow" | ///
						LAD14NM == "Havering" | LAD14NM == "Hillingdon" | ///
						LAD14NM == "Hounslow" | LAD14NM == "Kingston upon Thames" | ///
						LAD14NM == "Redbridge" | LAD14NM == "Richmond upon Thames" |  ///
						LAD14NM == "Sutton" 	| LAD14NM == "Waltham Forest"  


if "${tabsample}" == "borderin" {
	keep if treat == 1
}
if "${tabsample}" == "borderout" {
	keep if treat == 0 
}

order ${tabvarlist}	// for tables formatting
	
// Collapse
preserve
	collapse_vars post
	save "${tabledir}\descriptive_mean${tabsample}", replace
restore

preserve
	collapse_vars_count post
	rename v1 v1_count
	rename v2 v2_count
	save "${tabledir}\descriptive_count${tabsample}", replace
restore

// put together the table
collapse_vars_sd post
table_diff post

prepare_ttest_result
add_parenthesis *_sd 

table_diff_format
qui: latexTable

outsheet using "${tabledir}\descriptive_diff_perc${tabsample}_1.tex" ///
	if regexm(_varname, "Age|First time|quad Gross income"), replace noquote nonames
	
outsheet using "${tabledir}\descriptive_diff_perc${tabsample}_2.tex" ///
	if regexm(_varname,"value|Down|Equity|Interest|Maturity|fixed"), ///
	replace noquote nonames
	
outsheet using "${tabledir}\descriptive_diff_perc${tabsample}_3.tex" ///
	if regexm(_varname, "LTV|LTI|Payment"), replace noquote nonames
	
table_diff_last_row post
drop v3 	// ad hoc (to review)
qui: latexTable
outsheet using "${tabledir}\num_diff_perc${tabsample}.tex", ///
	replace noquote nonames
